package com.falcon.service.impl;

import java.util.List;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import cn.hutool.core.bean.BeanUtil;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.swan.mybatis.condition.Condition;
import com.falcon.mvc.domain.BasePager;
import com.falcon.core.utils.BeanUtils;

import com.falcon.domain.po.UserRolePO;
import com.falcon.domain.dos.UserRoleDO;
import com.falcon.domain.req.UserRoleQuery;
import com.falcon.domain.req.UserRoleRequest;

import com.falcon.mapper.IUserRoleMapper;
import com.falcon.service.IUserRoleService;


@Slf4j
@Service
public class UserRoleService implements IUserRoleService {

    @Autowired
    private IUserRoleMapper userRoleMapper;

    @Override
    public Long save(UserRoleRequest request){
        UserRolePO userRolePO = BeanUtil.copyProperties(request, UserRolePO.class);

        this.userRoleMapper.insertNotNull(userRolePO);

        return userRolePO.getId();
    }

    @Override
    public Boolean update(Long id, UserRoleRequest request){

        UserRolePO userRolePO = BeanUtil.copyProperties(request, UserRolePO.class);
        userRolePO.setId(id);

        return this.userRoleMapper.updateNotNull(userRolePO);
    }

    @Override
    public Boolean deleteById(Long id) {

        return this.userRoleMapper.deleteById(id);
    }

    @Override
    public UserRoleDO queryById(Long id){

        UserRolePO userRolePO = this.userRoleMapper.selectById(id);

        UserRoleDO userRoleDO = BeanUtil.copyProperties(userRolePO, UserRoleDO.class);

        return userRoleDO;
    }


    @Override
    public List<UserRoleDO> queryList(UserRoleQuery request){

        Condition condition = request.toCondition();

        List<UserRolePO> poList = this.userRoleMapper.selectList(condition);

        List<UserRoleDO> doList = BeanUtil.copyToList(poList, UserRoleDO.class);

        return doList;
    }

    @Override
    public BasePager<UserRoleDO> queryPager(UserRoleQuery request){


        PageInfo<UserRolePO> pageInfo = PageHelper.startPage(request.getPage(), request.getPageSize())
                .doSelectPageInfo(() -> this.userRoleMapper.selectList(request.toCondition()));

        return BeanUtils.toPager(pageInfo, UserRoleDO.class);
    }

    @Override
    public int batchDelete(List<Long> ids) {

        return this.userRoleMapper.deleteByIds(ids);
    }


}
